import 'dart:ffi';
import 'dart:io';
import 'dart:typed_data';
import 'package:ffi/ffi.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:path_provider/path_provider.dart';
import 'package:image/image.dart' as img;

// 定义动态库
final DynamicLibrary nativeLib = Platform.isAndroid
    ? DynamicLibrary.open("libcanvas_tool.so")
    : DynamicLibrary.process();

// 定义原生方法签名
typedef ConvertToEpaperNative = Int32 Function(
    Pointer<Uint32> image,
    Int32 width,
    Int32 height,
    Int32 displayType,
    Pointer<Utf8> path
);

typedef ConvertToEpaperDart = int Function(
    Pointer<Uint32> image,
    int width,
    int height,
    int displayType,
    Pointer<Utf8> path
);

// 加载原生方法
final convertToEpaper = nativeLib
    .lookupFunction<ConvertToEpaperNative, ConvertToEpaperDart>('cover_to_epaper');

Future<int> convertImageToEpaper(ByteData rgba, int width, int height, int displayType, String path) async {
  final pixelCount = width * height;
  final imagePtr = calloc<Uint32>(pixelCount); 
  print(' path : $path');
  // toast
  Fluttertoast.showToast(msg: 'path : $path');
  final pathPtr = path.toNativeUtf8();
  print(' pathPtr : $pathPtr');
  try {
    // 将RGBA数组转换为32位RGBA整数
    for (var i = 0; i < pixelCount; i++) {
      final r = rgba.getUint8(i * 4) >= 128 ? 255 : 0;     // R
      final g = rgba.getUint8(i * 4 + 1) >= 128 ? 255 : 0; // G
      final b = rgba.getUint8(i * 4 + 2) >= 128 ? 255 : 0; // B
      final a = rgba.getUint8(i * 4 + 3);                  // A
      
      // final r = rgba.getUint8(i * 4);     // R
      // final g = rgba.getUint8(i * 4 + 1); // G
      // final b = rgba.getUint8(i * 4 + 2); // B
      // final a = rgba.getUint8(i * 4 + 3);         
      imagePtr[i] = (a << 24) | (r << 16) | (g << 8) | b;

    //  if (r > 150 && r > g * 1.5 && r > b * 1.5) {
    //   if (r > 200 && g > 200 && b < 100) {
    // // 这是一个黄色像素
    
    //     imagePtr[i] = (a << 24) | (r << 16) | (g << 8) | b;    // 保持原始颜色
    //   } else {
    //     imagePtr[i] = (a << 24) | (255 << 16) | (0 << 8) | 0;  // 设置为纯红色
    //   }
    //     imagePtr[i] = (a << 24) | (r << 16) | (g << 8) | b;    // 保持原始颜色
    //   }
    }

    // 创建图片对象
    final image = img.Image(width: width, height: height);
    for (var i = 0; i < pixelCount; i++) {
      final value = imagePtr[i];
      final x = i % width;
      final y = i ~/ width;
      final a = (value >> 24) & 0xFF;
      final r = (value >> 16) & 0xFF;
      final g = (value >> 8) & 0xFF;
      final b = value & 0xFF;
      
      image.setPixel(x, y, img.ColorRgba8(r, g, b, a));
    }
    
    // 保存为PNG图片，使用应用文档目录
    // final appDir = await getTemporaryDirectory();
    // final debugImagePath = '${appDir.path}/debug_image.png';
    
    // final pngData = img.encodePng(image);
    // File(debugImagePath).writeAsBytesSync(pngData);
    
    // print('Debug image saved to: $debugImagePath');
    return convertToEpaper(imagePtr, width, height, displayType, pathPtr);
  } finally {
    calloc.free(imagePtr);
    calloc.free(pathPtr);
  }
} 